import 'dart:io';

import 'package:flutter/material.dart';
import 'package:video_trimmer/video_trimmer.dart';
import 'package:zz_video_maker/zz_video_maker.dart';

class VideoTrimmerView extends StatefulWidget {
  final EditedMedia editedMedia;
  final VoidCallback? callback;
  final VoidCallback? cancel;
  const VideoTrimmerView(
      {required this.editedMedia, this.callback, this.cancel, super.key});

  @override
  State<VideoTrimmerView> createState() => _VideoTrimmerViewState();
}

class _VideoTrimmerViewState extends State<VideoTrimmerView> {
  final Trimmer _trimmer = Trimmer();
  late double _videoStartValue = widget.editedMedia.startTime * 1000;
  late double _videoEndValue =
      (widget.editedMedia.startTime + widget.editedMedia.duration) * 1000;
  bool _isVideoPlaying = false;

  @override
  void initState() {
    _initVideoPlayerController();
    super.initState();
  }

  void _initVideoPlayerController() {
    _trimmer
        .loadVideo(
          videoFile: File(widget.editedMedia.mediaData.absolutePath),
        )
        .then((_) {});
  }

  void videoPlay() async {
    final bool playbackState = await _trimmer.videoPlaybackControl(
      startValue: _videoStartValue,
      endValue: _videoEndValue,
    );
    _isVideoPlaying = playbackState;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => videoPlay(),
      child: Container(
        color: Colors.black,
        child: Stack(
          fit: StackFit.expand,
          children: [
            Center(
              child: VideoViewer(
                trimmer: _trimmer,
              ),
            ),
            Container(
              alignment: Alignment.bottomCenter,
              padding: const EdgeInsets.symmetric(horizontal: 32),
              margin: const EdgeInsets.only(bottom: 88),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  TrimViewer(
                    trimmer: _trimmer,
                    viewerHeight: 50.0,
                    type: ViewerType.fixed,
                    editorProperties: TrimEditorProperties(
                      borderWidth: 2.5,
                      circleSize: 6.0,
                      circleSizeOnDrag: 9.0,
                      circlePaintColor: Colors.red,
                      borderPaintColor: Colors.red.withOpacity(0.75),
                      scrubberPaintColor: Colors.blue.withOpacity(0.40),
                    ),
                    areaProperties: TrimAreaProperties(),
                    durationStyle: DurationStyle.FORMAT_MM_SS,
                    durationTextStyle: const TextStyle(color: Colors.black),
                    // paddingFraction: 0,
                    maxVideoLength: Duration(
                        seconds:
                            (widget.editedMedia.mediaData.duration?.toInt() ??
                                    0) -
                                1),
                    viewerWidth: MediaQuery.of(context).size.width,
                    onChangeStart: (value) {
                      print('onChangeStart $value');
                      _videoStartValue = value;
                    },
                    onChangeEnd: (value) {
                      print('onChangeEnd $value');
                      _videoEndValue = value;
                    },
                    onChangePlaybackState: (value) => _isVideoPlaying = value,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      TextButton(
                          onPressed: () {
                            widget.editedMedia.startTime =
                                _videoStartValue * 0.001;
                            widget.editedMedia.duration =
                                (_videoEndValue - _videoStartValue) * 0.001;
                            widget.callback?.call();
                          },
                          child: Text('完成')),
                      TextButton(
                          onPressed: () {
                            widget.cancel?.call();
                          },
                          child: Text('取消')),
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
